﻿//创建时间: 2010-12-10
//创建作者: 李 城
//功能说明: 插件管理
//最后修改: 2010-12-10

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using System.Collections;
using Newtonsoft.Json;

namespace TRPOP.CMS.Web.Admin
{
    public partial class Plugin_save : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_strPagePermissionExtraKey = "Plugin_List";
        }
        #endregion
        
        #region 页面变量
        protected string m_Title = "编辑插件信息";
        protected string m_strRefer = "Plugin_list.aspx";
        protected string m_ConfigTypeKey = "Plugin";
        protected string m_strAction = "";
        #endregion

        #region 页面事件

        protected void Page_Load(object sender, EventArgs e)
        {
            m_strAction = RequestHelper.RequestString("ac");
            if (m_strAction.IndexOf("ajax") > -1)
            {
                AjaxMethod();
            }
            else
            {

                if (!IsPostBack)
                {
                    LoadBase();
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strMSG = "插件名称";
            if (!PageAssist.CheckForm(txtPluginName, ref strMSG)
                )
            {
                Alert(strMSG);
                return;
            }
            string strPluginKey = GetRequestViewState("PluginKey");
            if (RequestHelper.RequestString("PluginKey") == "")
            {
                strPluginKey = txtPluginKey.Text;
            }
            ArrayList oVarArray = SYSConfig.GetVarValue(strPluginKey, m_ConfigTypeKey);

            base.SaveLog(Plugin.LogKeys.OperObject.PluginOper, Plugin.LogKeys.OperResult.EditSubmit, strPluginKey);

            if (oVarArray != null && oVarArray.Count > 0)
            {
                oVarArray[1] = txtPluginName.Text.Trim();
                oVarArray[2] = rbtPluginEnable.SelectedValue;
                SubmitAlertAndThisNewLocation(SYSConfig.SaveVarValue(Config.VarAray.GetPluginAttribute(),
                                                                            oVarArray,
                                                                            m_ConfigTypeKey
                                                                            )
                                              , m_strRefer
                                              );
            }
            else if (strPluginKey!="")
            {
            
            }
            else
                base.ExecuteJs("window.location.href='" + m_strRefer + "'");
        }

        #endregion

        #region 页面方法

        public void LoadBase()
        {
            string strPluginKey = InitRequestViewState("PluginKey");
            ArrayList oVarArray = SYSConfig.GetVarValue(strPluginKey, m_ConfigTypeKey);
            if (oVarArray != null && oVarArray.Count>0)
            {
                txtPluginKey.Text = oVarArray[0].ToString().Trim();
                txtPluginName.Text = oVarArray[1].ToString().Trim();
                rbtPluginEnable.SelectedValue = oVarArray[2].ToString().Trim();
            }
            else
            {
                m_Title = "新增插件";
                txtPluginKey.Enabled = true;
                litPluginKey.Visible = true;
                txtPluginKey.Attributes.Add("required", "1");
                txtPluginKey.Attributes.Add("prompt", "关键字");
               // Alert("请通过正确的途径进入本页面");
                //base.ExecuteJs("window.location.href='" + m_strRefer + "'");
                return;
            }
        }
        /// <summary>
        /// ajax
        /// </summary>
        private void AjaxMethod()
        {
            JavaScriptObject result = new JavaScriptObject();
            JavaScriptObject items = new JavaScriptObject();
            string plugin_key = "";
            switch (m_strAction)
            {
                case "ajax_get_plugindata":
                    plugin_key = RequestHelper.RequestString("key");
                    if (plugin_key != "")
                    {
                        ArrayList oVarArray = SYSConfig.GetVarValue(plugin_key, m_ConfigTypeKey);
                        if (oVarArray != null && oVarArray.Count > 0)
                        {
                            for (int i = 0; i < oVarArray.Count; i++)
                            {
                                items.Add(i.ToString(), oVarArray[i]);
                            }
                            result.Add("item", items);
                            result.Add("result", "1");
                            result.Add("msg", "ok");
                        }
                        else
                        {
                            result.Add("result", "-1");
                            result.Add("msg", "没有获取到信息"+plugin_key);
                        }
                    }
                    else
                    {
                        result.Add("result", "-1");
                        result.Add("msg", "参数有误");
                    }
                    break;
                default:break;
            }

            Response.Write(JavaScriptConvert.SerializeObject(result));

            base.End();
        }
        #endregion
    }
}
